package com.itheima.edu.info.maneger.service;

import com.itheima.edu.info.maneger.dao.BaseStudentDao;
import com.itheima.edu.info.maneger.dao.StudentDao;
import com.itheima.edu.info.maneger.domain.Student;
import com.itheima.edu.info.maneger.factory.SystemFactory;

import javax.swing.plaf.synth.SynthTableUI;

public class StudentService {

    //StudentDao studentDao = new StudentDao();
    private BaseStudentDao studentDao = SystemFactory.getStudentDao();

    public boolean addStudent(Student student) {
        //将接受到的学生对象传递给StudentDao 接受boolean类型的返回值
        boolean flag = studentDao.addStudent(student);
        return flag;
    }

    //判断ID是否存在
    public boolean isExists(String id) {
        Student[] student = studentDao.findAllStudent();
        boolean exit = false;
        for (int i = 0; i < student.length; i++) {
            //student[i]
            if (student[i] != null && student[i].getId().equals(id)) {
                exit = true;
            }
        }
        return exit;

    }

    //查看学生方法
    public Student[] findAllStudent() {
        //从StudentDao中获取储存学生对象的数组
        Student[] students = studentDao.findAllStudent();
        boolean flag = false;
        //遍历数组，判断数组是否为空
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            //有信息
            return students;
        } else {
            //无信息
            return null;
        }
    }

    //删除学生方法
    public void deleteStudent(String id) {
        studentDao.deleteStudent(id);
    }


    public void updateStudent(String updateId, Student newstudent) {
        studentDao.updateStudent(updateId, newstudent);
    }
}












